package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.CompanyRecruitmentMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.CompanyRecruitment;
import edu.hubu.talentmanagementsystem.service.ICompanyRecruitmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class CompanyRecruitmentServiceImpl extends ServiceImpl<CompanyRecruitmentMapper, CompanyRecruitment> implements ICompanyRecruitmentService {

    private final MapperContext context;

    public CompanyRecruitmentServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getCompanyRecruitmentMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<CompanyRecruitment> insert(@EncodeParam CompanyRecruitment record) {
        int insert = context.getCompanyRecruitmentMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getCompanyRecruitmentMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<CompanyRecruitment> selectById(Integer id) {
        QueryWrapper<CompanyRecruitment> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<CompanyRecruitment> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(CompanyRecruitment::getCompanyRecruitmentId, id);
        return Optional.of(context.getCompanyRecruitmentMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<CompanyRecruitment> selectAll(String order) {
        QueryWrapper<CompanyRecruitment> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<CompanyRecruitment> select = getColumnWrapper(wrapper.lambda());
            return context.getCompanyRecruitmentMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<CompanyRecruitment> select = getColumnWrapper(wrapper.lambda());
        return context.getCompanyRecruitmentMapper().selectList(select);
    }

    @Override
    public Page<CompanyRecruitment> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<CompanyRecruitment> wrapper = new QueryWrapper<>();
        Page<CompanyRecruitment> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<CompanyRecruitment> select = getColumnWrapper(wrapper.lambda());
            return context.getCompanyRecruitmentMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<CompanyRecruitment> select = getColumnWrapper(wrapper.lambda());
        return context.getCompanyRecruitmentMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<CompanyRecruitment> getColumnWrapper(LambdaQueryWrapper<CompanyRecruitment> wrapper) {
        return wrapper.select(
                CompanyRecruitment::getWorkType
                    ,
                CompanyRecruitment::getEducationalDegree
                    ,
                CompanyRecruitment::getCompanyRecruitmentId
                    ,
                CompanyRecruitment::getCompanyRecruitmentCompanyId
                    ,
                CompanyRecruitment::getCompanyRecruitmentDepartmentId
                    ,
                CompanyRecruitment::getCompanyRecruitmentPositionId
                    ,
                CompanyRecruitment::getCompanyRecruitmentPositionRequirement
                    ,
                CompanyRecruitment::getCompanyRecruitmentPositionDuty
                    ,
                CompanyRecruitment::getCompanyRecruitmentPositionWelfare
                    ,
                CompanyRecruitment::getCompanyRecruitmentSalary
                    ,
                CompanyRecruitment::getCompanyRecruitmentWorkTypeId
                    ,
                CompanyRecruitment::getCompanyRecruitmentEducationalDegreeId
        );
    }

    @Override
    public Page<CompanyRecruitment> selectLikePaged( Integer currentPage,
             Integer pageSize, CompanyRecruitment record, String order) {
        Page<CompanyRecruitment> page = new Page<>(currentPage, pageSize);
        QueryWrapper<CompanyRecruitment> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getCompanyRecruitmentMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getCompanyRecruitmentMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<CompanyRecruitment> getQueryWrapper(QueryWrapper<CompanyRecruitment>wrapper, CompanyRecruitment record) {
        LambdaQueryWrapper<CompanyRecruitment> lambda = wrapper.lambda();
        if (record.getWorkType() != null) {
            lambda = lambda.eq(CompanyRecruitment::getWorkType,record.getWorkType());
        }
        if (record.getEducationalDegree() != null) {
            lambda = lambda.eq(CompanyRecruitment::getEducationalDegree,record.getEducationalDegree());
        }
        if (record.getCompanyRecruitmentId() != null) {
            lambda = lambda.eq(CompanyRecruitment::getCompanyRecruitmentId,record.getCompanyRecruitmentId());
        }
        if (record.getCompanyRecruitmentCompanyId() != null) {
            lambda = lambda.eq(CompanyRecruitment::getCompanyRecruitmentCompanyId,record.getCompanyRecruitmentCompanyId());
        }
        if (record.getCompanyRecruitmentDepartmentId() != null) {
            lambda = lambda.eq(CompanyRecruitment::getCompanyRecruitmentDepartmentId,record.getCompanyRecruitmentDepartmentId());
        }
        if (record.getCompanyRecruitmentPositionId() != null) {
            lambda = lambda.eq(CompanyRecruitment::getCompanyRecruitmentPositionId,record.getCompanyRecruitmentPositionId());
        }
        if (record.getCompanyRecruitmentWorkTypeId() != null) {
            lambda = lambda.eq(CompanyRecruitment::getCompanyRecruitmentWorkTypeId,record.getCompanyRecruitmentWorkTypeId());
        }
        if (record.getCompanyRecruitmentEducationalDegreeId() != null) {
            lambda = lambda.eq(CompanyRecruitment::getCompanyRecruitmentEducationalDegreeId,record.getCompanyRecruitmentEducationalDegreeId());
        }
        if (record.getCompanyRecruitmentPositionRequirement() != null) {
            lambda = lambda.like(CompanyRecruitment::getCompanyRecruitmentPositionRequirement,record.getCompanyRecruitmentPositionRequirement ());
        }
        if (record.getCompanyRecruitmentPositionDuty() != null) {
            lambda = lambda.like(CompanyRecruitment::getCompanyRecruitmentPositionDuty,record.getCompanyRecruitmentPositionDuty ());
        }
        if (record.getCompanyRecruitmentPositionWelfare() != null) {
            lambda = lambda.like(CompanyRecruitment::getCompanyRecruitmentPositionWelfare,record.getCompanyRecruitmentPositionWelfare ());
        }
        if (record.getCompanyRecruitmentSalary() != null) {
            lambda = lambda.like(CompanyRecruitment::getCompanyRecruitmentSalary,record.getCompanyRecruitmentSalary ());
        }
        return lambda;
    }

    public int count(CompanyRecruitment record) {
        return context.getCompanyRecruitmentMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<CompanyRecruitment> updateById(Integer id, @EncodeParam CompanyRecruitment record) {
        record.setCompanyRecruitmentId(id);
        int update = context.getCompanyRecruitmentMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<CompanyRecruitment> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<CompanyRecruitment> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(CompanyRecruitment::getCompanyRecruitmentId, id);
            return Optional.of(context.getCompanyRecruitmentMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }

    @Override
    public Page<CompanyRecruitment> pagingQueryCompanyRecruitmentByPositionSmallTagId(Integer currentPage, Integer pageSize, Integer positionSmallTagId) {
        Page<CompanyRecruitment> page = new Page<>(currentPage, pageSize);
        List<CompanyRecruitment> companyRecruitmentList = context.getCompanyRecruitmentMapper().pagingQueryCompanyRecruitmentByPositionSmallTagId(page, positionSmallTagId);
        page.setRecords(companyRecruitmentList);
        return page;
    }
}
